﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TomNet.IMSC.Core.Contracts.Decoration;
using TomNet.IMSC.Model.DbModels.Decoration;
using TomNet.Web.Mvc.UI;
using TomNet.Web.Mvc;
using System.ComponentModel;
using TomNet.Web.Mvc.Security;
using TomNet.IMSC.Web.Areas.Decoration.Models;
using TomNet.IMSC.Web.Filters;

namespace TomNet.IMSC.Web.Areas.Decoration.Controllers
{
    [Description("装修-装修公司顾问")]
    [GlobalAuthorization]
    public class DecorationConsultantController : BaseController
    {
        public IDecorationCompanyStoreContract DecorationCompanyStoreContract { get; set; }
        public IDecorationConsultantContract DecorationConsultantContract { get; set; }
        // GET: Decoration/DecorationConsultant
        #region 装修-装修公司顾问列表
        [Description("装修-装修公司顾问列表")]
        public ActionResult Index()
        {
            return View();
        }
        [Description("装修-装修公司顾问列表")]
        [HttpPost, AjaxOnly]
        public ActionResult GetList(int pageIndex, int pageSize, int id, string search = "")
        {
            var query = from q in DecorationConsultantContract.Entities
                        select new DecorationConsultantNew
                        {
                            Id = q.Id,
                            DecorationCompanyStoreID = q.DecorationCompanyStoreID,
                            DecorationCompanyStoreName = (from d in DecorationCompanyStoreContract.Entities where d.Id == q.DecorationCompanyStoreID select d.Name).FirstOrDefault(),
                            Name = q.Name,
                            Phone = q.Phone,
                            Duties = q.Duties
                        };
            //判断是否是链接传值
            if (id != -1)
            {
                query = query.Where(q => q.DecorationCompanyStoreID == id);
            }
            if (!string.IsNullOrWhiteSpace(search))
            {
                query = query.Where(m => m.Name.Contains(search));
            }

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderBy(m => m.Id);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<DecorationConsultantNew>().AsQueryable();

            //此处可以采用匿名对象 GridData<object>
            GridData<DecorationConsultantNew> gridData = new GridData<DecorationConsultantNew>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        }
        #endregion

        #region 装修-添加装修公司顾问
        [Description("装修-添加装修公司顾问")]
        public ActionResult Insert()
        {
            return View();
        }
        [Description("装修-添加装修公司顾问")]
        [HttpPost, AjaxOnly]
        public ActionResult InsertAsync(DecorationConsultant entity)
        {
            //所有AJAX的结果，返回统一数据格式
            var result = new AjaxResult();
            try
            {
                var query = DecorationConsultantContract.CheckExists(d => d.Name.Equals(entity.Name));
                if (query)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "已经存在相同的装修顾问";
                }
                else
                {
                    var count = DecorationConsultantContract.Insert(entity);
                    if (count > 0)
                    {
                        result.Type = AjaxResultType.Success;
                        result.Content = "录入成功";
                    }
                    else
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "录入失败";
                    }
                }
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
        #endregion

        #region 装修-修改装修公司顾问
        [Description("装修-修改装修公司顾问")]
        public ActionResult Edit(int id)
        {
            var entity = DecorationConsultantContract.GetByKey(id);
            //后台容错，有异常数据直接抛出。框架会自动跳转到错误页面。
            if (entity == null) throw new Exception("未找到该实体");
            ViewData["entity"] = entity;
            return View();
        }
        [Description("装修-修改装修公司顾问")]
        [HttpPost, AjaxOnly]
        public ActionResult EditAsync(DecorationConsultant entity)
        {
            var result = new AjaxResult();
            try
            {
                var query = DecorationConsultantContract.CheckExists(d => d.Name.Equals(entity.Name) && d.Id != entity.Id);
                if (query)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "已经存在相同的装修顾问";
                }
                else
                {
                    var count = DecorationConsultantContract.Update(entity);
                    if (count > 0)
                    {
                        result.Type = AjaxResultType.Success;
                        result.Content = "修改成功";
                    }
                    else
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "修改失败";
                    }
                }
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
        #endregion

        #region 装修-删除装修公司顾问
        [Description("装修-删除装修公司顾问")]
        [HttpPost, AjaxOnly]
        public ActionResult DeleteAsync(string ids)
        {
            var result = new AjaxResult();
            try
            {
                string[] idsStr = ids.Split(new char[] { ',' });
                int[] idsInt = Array.ConvertAll(idsStr, id => Convert.ToInt32(id));
                DecorationConsultantContract.Delete(d => idsInt.Contains(d.Id));
                //DecorationCompanyContract.UpdateDirect(m => idsInt.Contains(m.Id));

                result.Type = AjaxResultType.Success;
                result.Content = "删除成功";
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        } 
        #endregion
    }
}